/*
 * $Id: DatabaseTreeSelectionListenerDelegate.java 23 2009-03-31 23:37:36Z
 * aaron.day $
 */
package org.crazydays.gameplan.data.swing.tree;


import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.MutableTreeNode;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crazydays.gameplan.GamePlan;
import org.crazydays.gameplan.map.IGameMap;

/**
 * DatabaseTreeSelectionListenerDelegate
 */
public class DatabaseTreeSelectionListenerDelegate
        implements TreeSelectionListener
{
	/** log */
	protected static Log LOG =
	        LogFactory.getLog(DatabaseTreeSelectionListenerDelegate.class);

	/** game plan */
	protected GamePlan gamePlan;

	/**
	 * DatabaseTreeSelectionListenerDelegate constructor.
	 * 
	 * @param gamePlan GamePlan
	 */
	public DatabaseTreeSelectionListenerDelegate(GamePlan gamePlan)
	{
		setGamePlan(gamePlan);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan GamePlan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return GamePlan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * Open or bring map to the front.
	 * 
	 * @param e Event
	 * @see TreeSelectionListener#valueChanged(TreeSelectionEvent)
	 */
	public void valueChanged(TreeSelectionEvent e)
	{
		MutableTreeNode o =
		        (MutableTreeNode) e.getPath().getLastPathComponent();

		IGameMap gameMap = getGamePlan().getGameMap(o.toString());
		if (gameMap == null) {
			LOG.warn("Unable to find map: " + o);
		} else {
			getGamePlan().openGameMap(gameMap);
		}
	}
}
